
local function type_coin(inst)
    if inst:HasTag("npc_tag.upgraded") then
        -- print("Warning : type_coin upgraded")
        -- DoTaskInTime 0 的自动升级 和 外部升级冲突，会导致 升级2次，添加标记位屏蔽
        return
    end
    inst:AddTag("npc_tag.upgraded")
    inst:AddTag("npc_tag.shadowstrike") ---- 冲锋标记位，刷新的第一次攻击肯定是冲锋，先在这上标记。


    inst:RemoveTag("shadowminion")
    inst:RemoveTag("NOBLOCK")
    -- inst:RemoveTag("scarytoprey")
    inst:AddTag("monster")                    ------ 方便玩家攻击，不用强制攻击才能打到

    inst:AddTag("mirror_player_with_bottle")  ------ 方便和瓶子武器交互
    inst:AddTag("npc_task")                   ------ 不被守卫打

    -- inst.AnimState:OverrideSymbol("swap_object", "swap_glasscutter", "swap_glasscutter")
    inst.AnimState:OverrideSymbol("swap_object", "npc_item_bottle_with_coin_weapon", "npc_item_bottle_with_coin_weapon")


    inst.components.timer:PauseTimer("obliviate")
    inst.components.health:SetMaxHealth(TUNING.NPC_MOD_TEST_DEBUG and 100 or 2000)
    inst.components.health.currenthealth = inst.npc_everything_data:Get("health") or inst.components.health.maxhealth
    
    -- print("info : shadow npc health",inst.components.health.currenthealth)

    inst:ListenForEvent("attacked",function()
        inst:DoTaskInTime(0.1,function()
            inst.npc_everything_data:Set("health",inst.components.health.currenthealth)            
        end)
    end)

    local function CopySkin(inst,player)
        if inst.__skin_player == player then
            return
        end
        inst.components.skinner:CopySkinsFromPlayer(player)
        inst.AnimState:SetMultColour(1, 1, 1, .5)
        inst.__skin_player = player

        -----------------------------------------------
        ------ 针对Wolfgang 体型大小的应对
            if player.prefab == "wolfgang" then
                if player:HasTag("mightiness_wimpy") then
                    print("info mightiness_wimpy")
                    inst.Transform:SetScale(0.9,0.9,0.9)
                    -- inst.AnimState:SetScale(0.9,0.9,0.9)
                elseif player:HasTag("mightiness_mighty") then
                    print("info mightiness_mighty")
                    -- inst.AnimState:SetScale(1.2,1.2,1.2)    
                    inst.Transform:SetScale(1.2,1.2,1.2)
                end
            end
        -----------------------------------------------
    end
    -- local player = inst:GetNearestPlayer(true)
    local player = inst.npc_base_lib:GetNearestPlayer(true)

    if player and player:IsValid() and player:HasTag("npc_tag.isplayer") then
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
        CopySkin(inst,player)

        inst:DoPeriodicTask(1,function()
            local target = inst.npc_base_lib:GetNearestPlayer(true)
            if target then
                if inst.npc_base_lib:Distance_Targets(inst,target) > 30 then
                    local new_pt = inst.npc_base_lib:GetSpawnPoint(target,3)
                    if new_pt and new_pt.x then
                        inst.Transform:SetPosition(new_pt.x,0,new_pt.z)
                    end
                end
                inst.components.combat:SetTarget(target)
            end
        end)

        inst:DoPeriodicTask(10,function()   ------- 至少10s一次冲锋
            -- inst.components.combat.attackrange = 5 
            inst.components.combat:SetRange(5)
        end)

        inst:ListenForEvent("death",function()
            if not inst:HasTag("kill_by_bottle") then
                inst:AddTag("kill_by_bottle")
                inst:ListenForEvent("animqueueover",function()
                    SpawnPrefab("npc_item_bottle_with_coin").Transform:SetPosition(inst.Transform:GetWorldPosition())                    
                end)                
            end
        end)


        
    ----------------------------------------------------------------------------------------------------------------
    ------ 15s  内打不到任何玩家或者被打就传送到玩家旁边
        local time_out = TUNING.NPC_MOD_TEST_DEBUG and 10 or 15
        inst.___attack_player_timeout_fn = function(inst)
            -- print("___attack_player_timeout_fn",math.random(100))
            local temp_player = inst.npc_base_lib:GetNearestPlayer(true)
            if temp_player and temp_player:IsValid() and temp_player:HasTag("npc_tag.isplayer") then
                if inst.npc_base_lib:Distance_Targets(temp_player,inst) > 5 then
                    local pt = inst.npc_base_lib:GetSpawnPoint(temp_player,2) or Vector3(temp_player.Transform:GetWorldPosition())
                    inst.Transform:SetPosition(pt.x,0,pt.z)
                end
                -- inst.components.combat.attackrange = 5
                inst.components.combat:SetRange(5)
            end
        end

        inst.___attack_player_timeout_task = inst:DoTaskInTime(time_out,inst.___attack_player_timeout_fn)

        local function Restart_time_out_task(inst)
            if  inst.___attack_player_timeout_task then
                inst.___attack_player_timeout_task:Cancel()
                inst.___attack_player_timeout_task = nil
                inst.___attack_player_timeout_task = inst:DoTaskInTime(time_out,inst.___attack_player_timeout_fn)
            end
        end
        inst:ListenForEvent("onhitother",function(_,_table)
            -- print("onhitother",_table and _table.target)
            if _table and _table.target and _table.target:HasTag("npc_tag.isplayer") then
                Restart_time_out_task(inst)
            end
        end)

        inst:ListenForEvent("attacked",function(_,_table)
            if _table and _table.attacker and _table.attacker:HasTag("npc_tag.isplayer") then
                Restart_time_out_task(inst)
            end
        end)
    ----------------------------------------------------------------------------------------------------------------
    ------ 冲锋玩家就切皮肤
        -- 先上冲锋标记位
        inst.components.combat.SetRange__npc_old = inst.components.combat.SetRange
        inst.components.combat.SetRange = function(self,num)
            self:SetRange__npc_old(num)
            if num == 2 then
                self.inst:DoTaskInTime(0.3,function()
                    self.inst:RemoveTag("npc_tag.shadowstrike")
                    -- print("Remove  shadowstrike")
                end)
            elseif num == 5 then
                self.inst:AddTag("npc_tag.shadowstrike")
                -- print("AddTag  shadowstrike")
            end
        end

        inst:ListenForEvent("onhitother",function(_,_table)
            if inst:HasTag("npc_tag.shadowstrike")  and _table and _table.target 
            and _table.target:HasTag("npc_tag.isplayer") then
                CopySkin(inst,_table.target)
            end
        end)
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
    else
        inst:Remove()        
    end

end


AddPrefabPostInit(
    "shadowprotector",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end

        inst:AddComponent("npc_base_lib")
        inst:AddComponent("npc_everything_data")


        inst:ListenForEvent("upgrade.npc",function(_,the_type)
            inst.npc_everything_data:Set("upgrade.npc",the_type)
            if the_type == "coin" then
                type_coin(inst)
            end
        end)

        inst:DoTaskInTime(0,function()                                  ------- OnLoad Upgrade
            local the_type = inst.npc_everything_data:Get("upgrade.npc")
            -- print("info onload upgrade type",the_type)
            if the_type then
                inst:PushEvent("upgrade.npc",the_type)
            end
        end)

    end
)